<?php
/*
 * @Author: 李明
 * @Date: 2021-12-29 11:35:07
 * @LastEditTime: 2022-01-19 09:07:16
 * @LastEditors: 李明
 * @Description:
 * @FilePath: /CRM2.0-PHP/app/Http/Resources/Order/AuditOrder.php
 * @山东壹零贰肆信息科技有限公司
 */

namespace App\Http\Resources\Order;

use App\Http\Resources\OrderXcx;
use Illuminate\Http\Resources\Json\JsonResource;
use Nette\Utils\Arrays;

class AuditOrder extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        $audit_order['id'] = $this->id;
        $audit_order['branch_name'] = $this->branch->name;
        $audit_order['template_id'] = $this->template_id;
        $audit_order['families_money_records'] = $this->families_money_records;
        $audit_order['contract_no'] = $this->contract_no;
        $audit_order['renewal_contract_number'] = $this->renewal_contract_number;
        $audit_order['worker_name'] = $this->worker->name;
        $audit_order['worker_phone'] = $this->worker->contactNum;
        $audit_order['user_name'] = $this->user->name;
        $audit_order['user_phone'] = $this->user->contact_manner;
        $audit_order['id_number'] = $this->user->id_number;
        $audit_order['id_type'] = $this->user->id_type;
        $audit_order['bank_name'] = $this->bank_card->bank->bank_name;
        $audit_order['open_bank'] = $this->bank_card->bank_sum_name;
        $audit_order['bank_num'] = $this->bank_card->bank_num;
        $audit_order['sign_time'] = date('Y-m-d', strtotime($this->sign_time));
        $audit_order['valid_time'] = date('Y-m-d', strtotime($this->valid_time));
        $audit_order['account_time'] = date('Y-m-d', strtotime($this->account_time));
        $audit_order['contract_end_date'] =  date('Y-m-d', strtotime($this->contract_end_date));
        $audit_order['product_id'] = $this->product->id;
        $audit_order['product_name'] = $this->product->name;
        $pay_time_unit = $this->product->pay_time_unit ?? '';
        $pay_long =  $this->product->pay_long ?? '';
        $long_time =  $this->product->long_time ?? '';
        switch ($this->pay_type) { // 付款方式付款方式  2自然月  4到期 5不付款 4自定义
            case '5':
                $audit_order['product_pay_time'] = '无需支付';
                break;
            default:
                switch ($pay_time_unit) { // 付款间隔 1日、 2月、3年
                    case '1':
                        $audit_order['product_pay_time'] = $pay_long . '日/次';
                        $audit_order['product_long'] = $long_time . '日';
                        break;
                    case '2':
                        $audit_order['product_pay_time'] = $pay_long . '月/次';
                        $audit_order['product_long'] = $long_time . '月';
                        break;
                    case '3':
                        $audit_order['product_pay_time'] = $pay_long . '年/次';
                        $audit_order['product_long'] = $long_time . '年';
                        break;
                    default:
                        $audit_order['product_pay_time'] = '';
                        $audit_order['product_long'] = $long_time . '无效期限';
                        break;
                }
        }
        switch ($this->is_first) { // 是否为首个订单  1是新单 2否存量 3转续，
            case 1:
                $audit_order['order_type'] = "新单";
                $audit_order['user_type'] = "新客户";
                break;
            case 2:
                $audit_order['order_type'] = "存量";
                $audit_order['user_type'] = "老客户";
                break;
            case 3:
                $audit_order['order_type'] = "转续";
                $audit_order['user_type'] = "老客户";
                break;
            default:
                $audit_order['order_type'] = "未知";
                $audit_order['user_type'] = "未知";
                break;
        }
        $audit_order['order_status_str'] = OrderXcx::orderStatusToStr($this->order_status);;
        $audit_order['birthday'] = date('Y-m-d', strtotime($this->user->birthday));
        $order_money =  $this->order_money;
        $audit_order['order_money'] = $order_money;
        $audit_order['results_rate'] = $this->product->results_rate . "%";
        $audit_order['results_money'] = $this->results_money;
        $product_rate =  $this->product->rate;
        $audit_order['product_rate'] = $product_rate . "%";
        $audit_order['month_money'] = round($order_money * $product_rate / 100 / 12, 2);
        $image_urls = [];
        $order_imgs = $this->images;
        foreach ($order_imgs as $image) {
            $image_urls[] =  $image->url;
        }
        if (env('COMPANY', '3') != 3) { // 国邦盛世不展示这些图片
            $bank_card_imgs = $this->bank_card->images;
            $user_imgs = $this->user->images;
            foreach ($bank_card_imgs as $image) {
                $image_urls[] =  $image->url;
            }
            foreach ($user_imgs as $image) {
                $image_urls[] =  $image->url;
            }
        }

        $audit_order['image_urls'] =  $image_urls;
        // 判断通联认证状态
        $pay_status = '';
        $investment_way = $this->investment_way;
        switch ($investment_way) { // 投资状态  1常规 2通联未付款 3通联已付款
            case 1:
                $pay_status = '常规';
                break;
            case 2:
                $pay_status = '未付款';
                break;
            case 3:
                $pay_status = '已付款';
                break;
            default:
                $pay_status = '未知';
                break;
        }
        $audit_order['investment_way'] = $investment_way;
        $audit_order['pay_status'] = $pay_status;
        $audit_order['fdd_sign_status'] = $this->fdd_sign_status;
        $audit_order['contract_file'] = $this->contract_file;
        return $audit_order;
    }
}
